package com.qb.service.impl;

import com.qb.entity.EvaAndUnameAndBtitle;
import com.qb.mapper.BlogMapper;
import com.qb.mapper.EvaluateMapper;
import com.qb.mapper.UserMapper;
import com.qb.pojo.*;
import com.qb.service.EvaluateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class EvaluateServiceImpl implements EvaluateService {

    @Autowired
    private EvaluateMapper evaluateMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private BlogMapper blogMapper;

    //查询所有评论
    @Override
    public List findAll() {
        //ufk
        Integer getuFk = null;
        Integer getbFk = null;
        //查询所有评论
        List<Evaluate> evaluates = evaluateMapper.selectByExample(null);
        //创建Userlist集合
        List<User> users = null;
        List<Blog> blogs = null;
        ArrayList list = new ArrayList<>();
        //创建实体类
        EvaAndUnameAndBtitle evaNamBt = new EvaAndUnameAndBtitle();
        for (Evaluate evaluate : evaluates) {
            //创建实体类 将数据存入。
            EvaAndUnameAndBtitle eva = new EvaAndUnameAndBtitle();
            eva.setEid(evaluate.getEid());
            eva.setEcontent(evaluate.getEcontent());
            eva.setEtime(evaluate.getEtime());

            //判断ufk是否存在，存在则get
            if (evaluate.getuFk() != null) {
                getuFk = evaluate.getuFk();
                //创建UserEx;
                UserExample userExample = new UserExample();
                userExample.createCriteria().andUidEqualTo(getuFk);
                users = userMapper.selectByExample(userExample);
            }
            eva.setUser(users.get(0));
            //判断bfk是否存在，存在则get
            if (evaluate.getbFk() != null) {
                getbFk = evaluate.getbFk();
                //创建UserEx;
                BlogExample blogExample = new BlogExample();
                blogExample.createCriteria().andBidEqualTo(getbFk);
                blogs = blogMapper.selectByExample(blogExample);
            }
            eva.setBlog(blogs.get(0));
            list.add(eva);
        }
        return list;
    }

    //delete
    @Override
    public boolean deleteByEid(Integer eid) {
        return evaluateMapper.deleteByPrimaryKey(eid) > 0 ? true : false;
    }
    //模糊查询

    @Override
    public List findByEvaConent(String evaconent) {
//        System.out.println(evaconent);
//        List<EvaAndUnameAndBtitleByTitle> select = evaluateMapper.select(evaconent);
//        System.out.println(select);

//        EvaluateExample evaluateExample = new EvaluateExample();
//        evaluateExample.createCriteria().andEcontentLike("%" + evaconent + "%");
//        List<Evaluate> evaluateList = evaluateMapper.selectByExample(evaluateExample);
//        System.out.println(evaluateList);
//        if (evaluateList.size()>0){
//            Evaluate evaluate = evaluateList.get(0);
//            Integer getuFk = evaluate.getuFk();
//            Integer integer = evaluate.getbFk();
//        }
//        System.out.println("NOhave");
        //*************************
        //ufk
        Integer getuFk = null;
        Integer getbFk = null;
        //查询所有评论
        EvaluateExample evaluateExample = new EvaluateExample();
        evaluateExample.createCriteria().andEcontentLike("%" + evaconent + "%");
        List<Evaluate> evaluates = evaluateMapper.selectByExample(evaluateExample);
        //创建Userlist集合
        List<User> users = null;
        List<Blog> blogs = null;
        ArrayList list = new ArrayList<>();
        //创建实体类
        EvaAndUnameAndBtitle evaNamBt = new EvaAndUnameAndBtitle();
        for (Evaluate evaluate : evaluates) {
            //创建实体类 将数据存入。
            EvaAndUnameAndBtitle eva = new EvaAndUnameAndBtitle();
            eva.setEid(evaluate.getEid());
            eva.setEcontent(evaluate.getEcontent());
            eva.setEtime(evaluate.getEtime());

            //判断ufk是否存在，存在则get
            if (evaluate.getuFk() != null) {
                getuFk = evaluate.getuFk();
                //创建UserEx;
                UserExample userExample = new UserExample();
                userExample.createCriteria().andUidEqualTo(getuFk);
                users = userMapper.selectByExample(userExample);
            }
            eva.setUser(users.get(0));
            //判断bfk是否存在，存在则get
            if (evaluate.getbFk() != null) {
                getbFk = evaluate.getbFk();
                //创建UserEx;
                BlogExample blogExample = new BlogExample();
                blogExample.createCriteria().andBidEqualTo(getbFk);
                blogs = blogMapper.selectByExample(blogExample);
            }
            eva.setBlog(blogs.get(0));
            list.add(eva);
        }
        return list;
    }
}
